﻿
using GameFramework.Localization;
using System;
using System.Xml;
using UnityGameFramework.Runtime;

namespace LS.Localization
{
    /// <summary>
    /// XML 格式的本地化辅助器。
    /// </summary>
    public class XmlLocalizationHelper : DefaultLocalizationHelper
    {
        /// <summary>
        /// 解析字典。
        /// </summary>
        /// <param name="dictionaryString">要解析的字典字符串。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>是否解析字典成功。</returns>
        public override bool ParseData(ILocalizationManager localizationManager, string dictionaryString, object userData)
        {
            try
            {
                LanguageXMLReader reader = new LanguageXMLReader(dictionaryString);
                if(reader.Language != localizationManager.Language)
                {
                    return true;
                }
                Log.Info("XmlLocalizationHelper.ParseData");
                foreach (var item in reader.Values)
                {
                    if (!localizationManager.AddRawString(item.Key, item.Value))
                    {
                        Log.Warning("Can not add raw string with key '{0}' which may be invalid or duplicate.", item.Key);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception exception)
            {
                Log.Warning("Can not parse dictionary data with exception '{0}'.", exception.ToString());
                return false;
            }
        }
    }
}
